package com.ninthgroup.service.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ninthgroup.entity.Admin;
import com.ninthgroup.entity.Doctor;
import com.ninthgroup.entity.Permission;
import com.ninthgroup.entity.User;
import com.ninthgroup.mapper.PermissionMapper;
import com.ninthgroup.service.PermissionService;
import com.ninthgroup.util.PermissionUtil;
import com.ninthgroup.util.WebUtil;
import com.ninthgroup.vo.PermissionVo;
import com.ninthgroup.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author ： 1452863884@qq.com rcg
 * @date ：Created in 2022/10/14 11:12
 * @description：服务层权限接口的实现类
 * @modified By：1.0
 * @version:
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public Result findByAId() {

        Admin admin = (Admin) WebUtil.getSession().getAttribute("admin");

        Doctor doctor = (Doctor) WebUtil.getSession().getAttribute("doctor");

        User user = (User) WebUtil.getSession().getAttribute("user");
        /*Admin admin = new Admin();
        admin.setaId(1);*/

        if (admin !=null){
            List<Permission> permissions = permissionMapper.selectByAdminId(admin.getaId());
            List<Permission> firsts =PermissionUtil.firsts(permissions);
            return new Result(2000,"查询左侧菜单成功",firsts);
        }
        if (doctor !=null){
            List<Permission> permissions = permissionMapper.selectByDoctorId(doctor.getdId());
            List<Permission> firsts =PermissionUtil.firsts(permissions);
            return new Result(2000,"查询左侧菜单成功",firsts);
        }
        if (user !=null){
            List<Permission> permissions = permissionMapper.selectByUserId(user.getuId());
            List<Permission> firsts =PermissionUtil.firsts(permissions);
            return new Result(2000,"查询左侧菜单成功",firsts);
        }


        return new Result(500,"查询菜单失败");

    }


    @Override
    public Result findByRoleId(int roleId) {
        String pName="";
        //1.查询所有权限----而且必须有层级关系
        List<Permission> permissions = permissionMapper.selectAll(pName);

        List<Permission> firsts = PermissionUtil.firsts(permissions);
        //2.根据角色id查询该角色具有的权限id
        List<Integer> permissionIds = permissionMapper.selectByRoleId(roleId);


        Map<String,Object> map=new HashMap<>();
        map.put("firsts",firsts);
        map.put("permissionIds",permissionIds);
        return new Result(2000,"查询成功",map);
    }

    @Override
    public Result qr(Integer roleId, Integer[] permissionIds) {
        //1. 删除该角色具有的权限
        permissionMapper.deleteByRoleId(roleId);
        //2. 批量添加权限
        permissionMapper.batchPermission(roleId,permissionIds);
        return new Result(2000,"分配成功");
    }

    @Override
    public Result selectAll(Integer currentPage, Integer pageSize, PermissionVo permissionVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Permission> permissions = permissionMapper.selectAll(permissionVo.getpName());
        PageInfo<Permission> pageInfo=new PageInfo<>(permissions);
        Result result = new Result(200,"查询所有权限成功",pageInfo);
        return result;
    }

    @Override
    public Result insertPermission(Permission permission) {

        permissionMapper.insertSelective(permission);
        return new Result(200,"添加权限成功");
    }

    @Override
    public Result updatePermission(Permission permission) {
        permissionMapper.updateByPrimaryKeySelective(permission);
        return new Result(200,"修改权限成功");
    }

    @Override
    public Result deletePermission(Integer pId) {
        permissionMapper.deleteByPrimaryKey(pId);
        return new Result(200,"删除权限成功");
    }

}